package com.fenxiangle.yueding.feature.common.view;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.feature.login.view.LoginActivity;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;


public class SplashActivity extends BaseActivity {


    RxPermissions rxPermissions;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initSystemParams() {
//        super.initSystemParams();
        hideBottomUIMenu();
    }
    /**
     * 隐藏虚拟按键，并且设置成全屏
     */
    protected void hideBottomUIMenu(){
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE;
            decorView.setSystemUiVisibility(uiOptions);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
    @Override
    protected void initView() {
        rxPermissions=new RxPermissions(this);

    }

    @SuppressLint("CheckResult")
    @Override
    protected void initData() {
        onCodeClick();
    }

    public void onCodeClick() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                rxPermissions
                        .requestEach(Manifest.permission.CAMERA,
                                Manifest.permission.ACCESS_COARSE_LOCATION,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE
                        )
                        .subscribe(permission -> { // will emit 2 Permission objects
                            if (permission.granted) {
//                                rxPermissions
//                                        .request(permission.name)
//                                        .subscribe(granted -> {
//                                            if (granted) { // Always true pre-M
//                                                // I can control the camera now
//                                            } else {
//                                                // Oups permission denied
//                                            }
//                                        });
                            } else if (permission.shouldShowRequestPermissionRationale) {

                            } else {

                            }
                            if (AM.user().isLogin()) {
                                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            } else {
                                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                                intent.putExtra("is_home", true);
                                startActivity(intent);
                                finish();
                            }
                        });

            }
        }).start();
    }


}
